'''
Created on Apr 27, 2011

@author: mwarren1
'''

import os
import sys
import wx

from maildroplibs import DEBUGSTREAM, ENABLEDEBUG
from wx.tools.dbg import Logger

debug = Logger('StartupSettings')
debug.SetStream(DEBUGSTREAM)
debug(enable = ENABLEDEBUG)

    
def startupEnabled():
    if 'wxMSW' in wx.PlatformInfo:
        path = os.path.join(os.getenv('APPDATA'),
                        "Microsoft",
                        "Windows",
                        "Start Menu",
                        "Programs",
                        "Startup",
                        "MailDrop.lnk")
        debug('Startup Enabled: ' + str(os.path.exists(path)))
        return os.path.exists(path)
    elif 'wxGTK' in wx.PlatformInfo:
        path = os.path.join(os.getenv('HOME'),
                            ".config",
                            "autostart",
                            "maildrop.desktop")
        debug('Startup Enabled: ' + str(os.path.exists(path)))
        return os.path.exists(path)
    elif 'wxMac' in wx.PlatformInfo:
        return False
    else:
        return False

def enableStartup(doEnable = True):
    if doEnable:
        if not startupEnabled():
            debug('Enabling Run on Startup')
            if 'wxMSW' in wx.PlatformInfo:
                __enableWinStartup()
            elif 'wxGTK' in wx.PlatformInfo:
                __enableLinuxStartup()
            elif 'wxMac' in wx.PlatformInfo:
                __enableMacStartup()
            else:
                pass
        else:
            debug('Enable Startup Called when Already Enabled')
    else:
        disableStartup()

def disableStartup():
    if startupEnabled():
        debug('Disabling Run on Startup')
        if 'wxMSW' in wx.PlatformInfo:
            __disableWinStartup()
        elif 'wxGTK' in wx.PlatformInfo:
            __disableLinuxStartup()
        elif 'wxMac' in wx.PlatformInfo:
            __disableMacStartup()
        else:
            pass
    else:
        debug('Disable Startup Called when Already Disabled')
    
def __enableWinStartup():
    from win32com.client import Dispatch
    
    #For XP or older
    if sys.getwindowsversion()[0] <= 5:
        path = os.path.join(os.getenv('USERPROFILE'),
                            "Start Menu",
                            "Programs",
                            "Startup",
                            "MailDrop.lnk")
    #For Vista or newer
    else:
        path = os.path.join(os.getenv('APPDATA'),
                            "Microsoft",
                            "Windows",
                            "Start Menu",
                            "Programs",
                            "Startup",
                            "MailDrop.lnk")

    wDir = os.getcwd()
    target = os.path.join(wDir, "MailDrop.exe")
    icon = os.path.join(wDir, "box.ico")
     
    shell = Dispatch('WScript.Shell')
    shortcut = shell.CreateShortCut(path)
    shortcut.Targetpath = target
    shortcut.WorkingDirectory = wDir
    shortcut.IconLocation = icon
    
    shortcut.save()   
    
def __disableWinStartup():
    path = os.path.join(os.getenv('APPDATA'),
                        "Microsoft",
                        "Windows",
                        "Start Menu",
                        "Programs",
                        "Startup",
                        "MailDrop.lnk")
    os.remove(path)
    
    
def __enableLinuxStartup():
    path = os.path.join(os.getenv('HOME'),
                            ".config",
                            "autostart",
                            "maildrop.desktop")
    
    desktopfile = "[Desktop Entry]\r\n" + \
                    "Name=MailDrop\r\n" + \
                    "GenericName=File Downloader\r\n" + \
                    "Comment=Upload Files to Dropbox via Email\r\n" + \
                    "Exec=pymaildrop\r\n" + \
                    "Terminal=false\r\n" + \
                    "Type=Application\r\n" + \
                    "Icon=maildrop\r\n" + \
                    "Categories=Network;FileTransfer;\r\n" + \
                    "StartupNotify=false"
                    
    debug('Writing Desktop Autostart file to: ' + str(path))                
    with open(path, 'w') as file:
        file.write(desktopfile)
    
    
def __disableLinuxStartup():
    path = os.path.join(os.getenv('HOME'),
                            ".config",
                            "autostart",
                            "maildrop.desktop")
    os.remove(path)

def __enableMacStartup():
    pass
def __disableMacStartup():
    pass
        
if __name__ == "__main__":
    #test functions by toggling startup
    enableStartup(not startupEnabled())